from smart_server import SmartServer

# 创建服务器实例（指定前端文件目录）
server = SmartServer()

# 运行服务器
server.run(host="0.0.0.0", port=8000)
# from smart_choice.datanodes import DataNodes

# def json_to_datanodes(json_data, payoff_fn):
#     nodes = DataNodes()
#     for node in json_data["nodes"]:
#         name = node["name"]
#         node_type = node["type"]
#         branches = node.get("branches", [])

#         if node_type == "decision":
#             # 决策节点
#             branches_tuple = [
#                 (b["label"], float(b["value"]), b["next"])
#                 for b in branches
#             ]
#             nodes.add_decision(name=name, branches=branches_tuple, maximize=True)

#         elif node_type == "chance":
#             # 概率节点
#             branches_tuple = [
#                 (b["label"], float(b["probability"]), float(b["value"]), b["next"])
#                 for b in branches
#             ]
#             nodes.add_chance(name=name, branches=branches_tuple)

#         elif node_type == "terminal":
#             # 终点节点
#             nodes.add_terminal(name=name, payoff_fn=payoff_fn)

#         else:
#             raise ValueError(f"Unsupported node type: {node_type}")

#     return nodes


# # 你的 payoff 函数（和你上面定义的一样）
# def payoff_fn(values, probabilities, branches):
#     bid = values.get("bid", 0)
#     competitor_bid = values.get("competitor_bid", 0)
#     cost = values.get("cost", 0)
#     return (bid - cost) * (1 if bid < competitor_bid else 0)

# # 假设你已经加载了 JSON 数据为 json_data
# import json

# json_data = {
#   "nodes": [
#     {
#       "name": "bid",
#       "type": "decision",
#       "branches": [
#         {
#           "label": "low",
#           "next": "competitor_bid",
#           "value": 300
#         },
#         {
#           "label": "medium",
#           "next": "competitor_bid",
#           "value": 500
#         },
#         {
#           "label": "high",
#           "next": "competitor_bid",
#           "value": 700
#         },
#         {
#           "label": "no-bid",
#           "next": "profit",
#           "value": 0
#         }
#       ]
#     },
#     {
#       "name": "competitor_bid",
#       "type": "chance",
#       "branches": [
#         {
#           "label": "low",
#           "next": "cost",
#           "value": 400,
#           "probability": 0.35
#         },
#         {
#           "label": "medium",
#           "next": "cost",
#           "value": 600,
#           "probability": 0.5
#         },
#         {
#           "label": "high",
#           "next": "cost",
#           "value": 800,
#           "probability": 0.15
#         }
#       ]
#     },
#     {
#       "name": "cost",
#       "type": "chance",
#       "branches": [
#         {
#           "label": "low",
#           "next": "profit",
#           "value": 200,
#           "probability": 0.25
#         },
#         {
#           "label": "medium",
#           "next": "profit",
#           "value": 400,
#           "probability": 0.5
#         },
#         {
#           "label": "high",
#           "next": "profit",
#           "value": 600,
#           "probability": 0.25
#         }
#       ]
#     },
#     {
#       "name": "profit",
#       "type": "terminal",
#       "branches": []
#     }
#   ]
# }

# # 构建 DataNodes
# nodes = json_to_datanodes(json_data, payoff_fn)

# # 可选：创建决策树进行评估
# from smart_choice.decisiontree import DecisionTree
# tree = DecisionTree(nodes=nodes)
# tree.evaluate()
# tree.rollback()
# test = tree.display()

# dot = tree.plot()
# dot.save("tree_output.dot")



# json.dump(test,open("./tmp.txt",mode='w'))
